﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DeviceFinder.Models;

using System.Web;
using System.Web.Mvc;
using DeviceFinder.DAL;
using System.Text;

namespace DeviceFinder.Controllers
{
    public class PostFileController : ApiController
    {
        // GET api/postfile
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/postfile/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/postfile
        public string Post([FromBody]UploadFileModel value, string AccessToken)
        {
            string tokenStr = AccessToken;
            Dictionary<string, TokenModel> tkDict = HttpContext.Current.Application[OAuthManager.name] as Dictionary<string, TokenModel>;
            TokenModel tk = null;
            try
            {
                tk = tkDict[tokenStr];
                if (tk != null && tk.isAccessToken == true)
                {
                    UploadFileDAL upDAL = new UploadFileDAL();
                    UTF8Encoding encoding = new UTF8Encoding();
                    UPLOADFILE file = new UPLOADFILE()
                    {
                        IDDEVICE = tk.DeviceId,
                        DATE = DateTime.Now,
                        LAT = value.Lat,
                        LONG = value.Long,
                        PATH = value.Path,
                        DATA = value.Data
                    };
                    upDAL.AddFileToDB(file);
                    return file.ID.ToString();
                }
                else
                {
                    return "-1";
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        // PUT api/postfile/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/postfile/5
        public void Delete(int id)
        {
        }
    }
}
